package com.icewld.alarmofremindingme;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import android.widget.Toast;

import java.sql.Time;
import java.util.Calendar;
import java.util.TimerTask;

public class TimetriggerService extends Service {

    private TimetiggerReceiver ttr;
    private PowerManager.WakeLock wakeLock;
    private int tt_length;
    private int [][] timetable;
    private TimePlan tp;
    private SoundMaker sm;

    public TimetriggerService() {
    }

    @Override
    public void onCreate(){
        //保证服务不被暂停
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"myapp:MyWakelockTag");
        wakeLock.acquire();
        ttr = new TimetiggerReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        registerReceiver(ttr, filter);
        sm = new SoundMaker(getApplicationContext());
        tp = new TimePlan();
        timetable = tp.WriteTimeTable();
        tt_length = tp.GetttLength();
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy(){
        unregisterReceiver(ttr);
        ttr = null;
        super.onDestroy();
    }

    //判断是否发出提示
    public void JugdeMakeSound() {
        //得到当前的时间
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        //1 日 2 一 3 二 .....
        int week = c.get(Calendar.DAY_OF_WEEK);
        int data = -2;
        //课数
        int course_int = 0;
        //Timetable[0] 小时； Timetable[1] 分钟； Timetable[2] 数据
        for (int t = 0; t < tt_length; t++) {
            if (hour == timetable[0][t] && minute == timetable[1][t]) {
                data = timetable[2][t];
                course_int = t;
            }
        }
        System.out.println("现在是" + hour + "小时" + minute + "分钟" + ",星期" + (week - 1) + ",data:" + data + ",course_int:" + course_int);
        //if data is avaliable, make a sound
        switch (data) {
            case 0:
                if (!tp.WriteCourseTable()[course_int / 2][week - 1].equals("N")) {
                    sm.MakeSound("learn");
                    //播放课时声
                    sm.MakeSound(tp.WriteCourseTable()[course_int / 2][week - 1]);
                }
                break;
            case 1:
                if (!tp.WriteCourseTable()[course_int / 2 - 1][week - 1].equals("N")) {
                    sm.MakeSound("enjoy");
                }
                break;
            case 2:
                sm.MakeSound("getup");
                if(week == 1 || week == 7){
                    sm.MakeSound("ZM");
                }
                else {
                    sm.MakeSound("GR");
                }
                break;
            default:
                break;
        }
    }

    private class TimetiggerReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context arg0, Intent arg1){
            //接收到广播后的逻辑
            JugdeMakeSound();
            System.out.println("已进行JMS");
        }
    }
}
